package com.viphuli.app.tool.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.ArrangeRecord;
import com.viphuli.app.tool.common.Constants.ArrangeRecordType;
import com.viphuli.app.tool.common.MyApp;

public class ArrangeRecordAdapter extends BaseAdapter {
	private List<ArrangeRecord> list;

	public ArrangeRecordAdapter(List<ArrangeRecord> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int index) {
		return list.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null || convertView.getTag() == null) {
			convertView = ViewUtils.inflateView(MyApp.getInstance(), R.layout.arrange_record_list_item);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		ArrangeRecord recordInfo = list.get(position);
		
		holder.name.setText(recordInfo.getName());
		holder.desc.setText(recordInfo.getDesc());
		SimpleDateFormat formatDay = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		holder.time.setText(formatDay.format(recordInfo.time()));
		
		//审核人&操作人
		if (recordInfo.getArrangeInfoList().iterator().next().getVerifier() != null) {
			
			String userName = recordInfo.getArrangeInfoList().iterator().next().getVerifier().getUserName();
			holder.user.setText("审核人："+userName);
		
		}else{
			
			String userName = recordInfo.getArrangeInfoList().iterator().next().getFromUser().getUserName();
			holder.user.setText("操作人："+userName);
			
		}

		ArrangeRecordType type = ArrangeRecordType.getType(recordInfo.getType());

		if (ArrangeRecordType.arrange.equals(type)) {

			holder.img.setImageResource(R.drawable.ic_arrange_record_arrange);

		} else if (ArrangeRecordType.arrange_cancel.equals(type)) {

			holder.img.setImageResource(R.drawable.ic_arrange_record_arrange);

		} else if (ArrangeRecordType.exchange.equals(type)) {

			holder.img.setImageResource(R.drawable.ic_arrange_record_exchange);

		} else if (ArrangeRecordType.exchange_cancel.equals(type)) {

			holder.img.setImageResource(R.drawable.ic_arrange_record_exchange);

		} else if (ArrangeRecordType.replace.equals(type)) {

			holder.img.setImageResource(R.drawable.ic_arrange_record_exchange);

		} else if (ArrangeRecordType.replace_cancel.equals(type)) {

			holder.img.setImageResource(R.drawable.ic_arrange_record_exchange);

		}


		return convertView;
	}

	protected class ViewHolder {

		@Bind(R.id.id_home_item_icon_img)
		ImageView img;
		
		@Bind(R.id.id_home_item_name)
		TextView name;

		@Bind(R.id.id_home_item_time)
		TextView time;

		@Bind(R.id.id_home_item_desc)
		TextView desc;

		@Bind(R.id.id_home_item_user)
		TextView user;

		public ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}
}
